//index.js
var app = getApp();
Page({
  data: {
    // text:"这是一个页面"
    recommendedIndex: 4,
    reachBottomStatus: 1,
    browseList: [],
    emptyStatus: 1,
    lastId: 0
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

  },
  onReady: function () {
    // 页面渲染完成
    this.loadData(this.data.lastId);
  },
  onShow: function () {
    // 页面显示

  },
  onReachBottom: function () {
    // Do something when page reach bottom.
    var that = this;
    if(that.data.reachBottomStatus == 2){
      var lastId = that.data.browseList[that.data.browseList.length-1].id;
      that.loadData(lastId);
    }

  },
  onPullDownRefresh: function (){
    var that = this;
    that.setData({
      browseList: []
    });
    this.loadData(0);
    wx.stopPullDownRefresh();
  },
  loadData: function (id) {
    var that = this;
    app.doRequest('myBrowseList', {
      nextId: id,
      pageSize: 10
    }, function (data) {
      var browseListItems = data;
      var browseLoadedListItems = that.data.browseList;
      if (browseListItems.length > 0) {
        browseListItems.forEach(function (val) {
          val.goods.priceArray = val.goods.price.split('.');
          val.goods.rangesArray = [];
          val.goods.rangesArrayTwo = [];
          val.rangesStatus = 1;
          val.goods.ranges.forEach(function (resDetail, index) {
            if(resDetail.indexOf(':') > 0){
              resDetail = (resDetail.split(':'));
            }
            if(resDetail.indexOf('：') > 0){
              resDetail = (resDetail.split('：'));
            }
            if (index <= 1) {
              val.goods.rangesArrayTwo.push(resDetail);
            }
            val.goods.rangesArray.push(resDetail);

          });
          browseLoadedListItems.push(val);
        });

        that.setData({
          browseList: browseLoadedListItems
        });
        that.data.reachBottomStatus = 2;
      } else {
        that.data.reachBottomStatus = 1;
      }
      if(that.data.browseList.length > 0){
        that.setData({
          emptyStatus: 1
        });
      }else{
        that.setData({
          emptyStatus: 2
        });
      }

    })
  },
  gotoMyComplaints: function (event) {
    var goodsId = event.currentTarget.dataset.goodsid;
    wx.navigateTo({ url: '/pages/my/complaints/index?Id=' + goodsId + '' });
  },
  deleteAllSkimItem: function (event) {
    var that = this;

    wx.showModal({
      title: '提示',
      content: '是否清空浏览记录？',
      success: function (res) {
        if (res.confirm) {
          app.doRequest('myBrowseList_Clean', {
          }, function (data) {
            that.setData({
              browseList: [],
              emptyStatus: 2
            });
          })
        }
      }
    })
  },
  gotoDetail: function (event) {
    var goodsId = event.currentTarget.dataset.goodsid;
    wx.navigateTo({ url: '/pages/product/detail/index?Id=' + goodsId + '' });
  },
  addCollection: function (event) {
    var that = this;
    var goodsId = event.currentTarget.dataset.goodsid;
    var goodsIndex =event.currentTarget.dataset.index;
    var goodsItemsList = that.data.browseList;
    //console.log(goodsId);
    app.doRequest('goodsCollect', {
      goodsId: goodsId
    }, function (data) {
      console.log(data);
      if (data.operateType == 'collect') {
        goodsItemsList[goodsIndex].goods.isCollect = 1;
        app.showToastMsg('收藏成功',function (){
          that.setData({
            browseList: goodsItemsList
          });
        });
      }
      if (data.operateType == 'uncollect') {
        goodsItemsList[goodsIndex].goods.isCollect = 0;
        app.showToastMsg('取消收藏',function (){
          that.setData({
            browseList: goodsItemsList
          });
        });
      }
    })
  },
  addGoodsGood: function (event) {
    var that = this;
    var goodsId = event.currentTarget.dataset.goodsid;
    var goodsIndex =event.currentTarget.dataset.index;
    var goodsItemsList = that.data.browseList;
    //console.log(goodsId);
    app.doRequest('goodsGood', {
      goodsId: goodsId
    }, function (data) {
      console.log(data);
      if (data.operateType == 'good') {
        goodsItemsList[goodsIndex].goods.isGood = 1;
        goodsItemsList[goodsIndex].goods.goodNumber = parseInt(goodsItemsList[goodsIndex].goods.goodNumber) + 1;
        app.showToastMsg('点赞成功',function (){
          that.setData({
            browseList: goodsItemsList
          });
        });
      }
      if (data.operateType == 'ungood') {
        goodsItemsList[goodsIndex].goods.isGood = 0;
        goodsItemsList[goodsIndex].goods.goodNumber = parseInt(goodsItemsList[goodsIndex].goods.goodNumber) - 1;
        app.showToastMsg('取消点赞',function (){
          that.setData({
            browseList: goodsItemsList
          });
        });
      }
    })

  },
  deleteSkimItem: function (event) {
    var that = this;
    var goodsId = event.currentTarget.dataset.goodsid;
    var goodsIndex =event.currentTarget.dataset.idx;
    var browseLoadedListItems = that.data.browseList;
    wx.showModal({
      title: '提示',
      content: '是否删除此条浏览记录？',
      success: function (res) {
        if (res.confirm) {
          app.doRequest('myBrowseList_Delete', {
            goodsId: goodsId
          }, function (data) {
            browseLoadedListItems.splice(goodsIndex,1);
            that.setData({
              browseList: browseLoadedListItems
            });
            if(that.data.browseList.length > 0){
              that.setData({
                emptyStatus: 1
              });
            }else{
              that.setData({
                emptyStatus: 2
              });
            }
          })
        }
      }
    })
  },
  seeMoreMsg: function (res) {
    var that = this;
    var moreIndex = res.currentTarget.dataset.index;
    var productListItems = that.data.browseList;

    if (productListItems[moreIndex].rangesStatus == 1) {
      productListItems[moreIndex].rangesStatus = 2;
      that.setData({
        browseList: productListItems
      });
    } else {
      productListItems[moreIndex].rangesStatus = 1;
      that.setData({
        browseList: productListItems
      });
    }
  }
})